package test01;


import java.sql.*;

/**
 * @author Davey chan
 */
public class JDBCtest {
    public static void main(String[] args) throws Exception {
//        注册驱动
        Class.forName("com.mysql.jdbc.Driver");
//        获取连接
        String url="jdbc:mysql:///data02?serverTimezone=GMT%2B8";
        String username="root";
        String password="123456";
        Connection conn=DriverManager.getConnection(url,username,password);

        String name="小刚";
        String pwd="1234";

        //定义查询语句
        String sql="select * from  account where username=? and password=?";
//获取执行对象
//Statement stmt=conn.createStatement();
        PreparedStatement pstmt=conn.prepareStatement(sql);
// 这个是对Statement的强化，避免了登入的bug
//执行语句
    //        int count= pstmt.executeUpdate(sql);
    //       System.out.println(count);
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
//         执行sql
        ResultSet rs=pstmt.executeQuery();
        if(rs.next()){
            System.out.println("登录成功");

        }else{
            System.out.println("登录失败");
        }
//        释放资源
        pstmt.close();
        conn.close();
    }
}
